class_name Shop
extends ColorRect

@onready var item_list = %装备
@onready var refresh: Button = %Refresh
@onready var current_gold: Label = %当前金币
@onready var player_detail: RichTextLabel = %player_detail
@onready var animation_player: AnimationPlayer = %AnimationPlayer

var player_stats: PandoraEntity
var choose_id = null
var item_list_index

const item_icon = preload("uid://bniaqlyuyi5c4")

func _ready() -> void:
	self.visible = false
	ItemManager.item_pool.generate_item_pool()
	visibility_changed.connect(on_visibility_changed)
	player_stats = Utils.player.player_stats
	player_stats.changed.connect(_on_player_stats_changed)
	EventBus.subscribe("show_shop", on_show_shop)
	EventBus.subscribe("hide_shop", on_hide_shop)
	EventBus.subscribe("refresh_shop", on_refresh_shop)
	for child in item_list.get_children():
		child.queue_free()
	refresh_shop()
	_on_player_stats_changed()

## 新手剑、新手钱币、新手盾、新手法杖、新手智慧
var exclude_first_round: Array[String] = ["91", "95", "132", "133", "134"]
func refresh_shop():
	var generated_ids: Array[String] = []
	var _current_gold = player_stats.get_integer("gold")
	while generated_ids.size() < 4:
		var rarity := Utils.get_random_rarity_for_level()
		var new_item: ItemIcon = item_icon.instantiate()
		var gen_stats = ItemManager.item_pool.get_random_unit(rarity)
		new_item.item_stats = gen_stats
		if generated_ids.has(gen_stats.get_entity_id()):
			continue
		else:
			## 如果当前关卡大于0就不能刷到新手类武器
			if Utils.now_level > 0 and exclude_first_round.has(gen_stats.get_entity_id()):
				continue
			generated_ids.append(gen_stats.get_entity_id())
		new_item.buy_item.connect(_on_buy_item)
		item_list.add_child(new_item)
		if _current_gold < new_item.cost:
			new_item.buy_button.disabled = true
			
## 刷新商店
func _on_refresh_pressed() -> void:
	print("刷新一次商店")
	Utils.set_player_stats_value("gold", -50)
	for child in item_list.get_children():
		child.queue_free()
	refresh_shop()
	GameManager.refresh_count += 1
	
# 关闭商店
func _on_next_pressed() -> void:
	self.visible = false

## 当玩家属性改变的时候
func _on_player_stats_changed() -> void:
	# 如果没有足够金币，将当前选择界面下的购买按钮和刷新按钮disabled
	var _current_gold = player_stats.get_integer("gold")
	current_gold.text = str(_current_gold)
	var has_enough_gold = _current_gold >= 50
	refresh.disabled = not has_enough_gold
	for child: ItemIcon in item_list.get_children():
		if child.buy_button:
			if _current_gold < child.cost:
				child.buy_button.disabled = true
			else:
				child.buy_button.disabled = false

## 当商店打开
func on_visibility_changed() -> void:
	if is_visible_in_tree():
		get_tree().paused = true
		print("打开商店")
	else:
		get_tree().paused = false
		EventBus.push_event("player_bag_hide", [])
		print("关闭商店")

func on_show_shop() -> void:
	if self.visible == true:
		return
	animation_player.play("show")
	EventBus.push_event("player_bag_show", [])

func on_hide_shop() -> void:
	if self.visible == false:
		return
	animation_player.play("hide")
	EventBus.push_event("player_bag_hide", [])

func on_refresh_shop() -> void:
	for child in item_list.get_children():
		child.queue_free()
	refresh_shop()
	
func _on_buy_item() -> void:
	pass
